package net.qhzw.vip.qiyu.listener;

import lombok.AllArgsConstructor;
import net.qhzw.vip.qiyu.dto.ShopCartItemDto;
import net.qhzw.vip.qiyu.dto.ShopCartOrderDto;
import net.qhzw.vip.qiyu.entity.Product;
import net.qhzw.vip.qiyu.entity.Sku;
import net.qhzw.vip.qiyu.entity.UserAddr;
import net.qhzw.vip.qiyu.evnet.ConfirmOrderEvent;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.order.ConfirmOrderOrder;
import net.qhzw.vip.qiyu.param.OrderParam;
import net.qhzw.vip.qiyu.service.*;
import net.qhzw.vip.qiyu.util.Arith;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;

/**
 * 确认订单信息时的默认操作
 */
@Component("defaultConfirmOrderListener")
@AllArgsConstructor
public class ConfirmOrderListener {

    @Autowired
    private UserAddrService userAddrService;

    @Autowired
    private TransportManagerService transportManagerService;

    @Autowired
    private ProductService productService;

    @Autowired
    private SkuService skuService;


    @Autowired
    private MallUserVipService mallUserVipService;

    /**
     * 计算订单金额
     */
    @EventListener(ConfirmOrderEvent.class)
    @Order(ConfirmOrderOrder.DEFAULT)
    public void defaultConfirmOrderEvent(ConfirmOrderEvent event) {
        ShopCartOrderDto shopCartOrderDto = event.getShopCartOrderDto();
        OrderParam orderParam = event.getOrderParam();
        double total = 0;
        int totalCount = 0;
        for (ShopCartItemDto shopCartItem : event.getShopCartItems()) {
            // 获取商品信息
            Product product = productService.getProductByProdId(shopCartItem.getProdId());
            // 获取sku信息
            Sku sku = skuService.getSkuBySkuId(shopCartItem.getSkuId());
            if (product == null || sku == null) {
                throw new ApiException("购物车包含无法识别的商品");
            }
            if (product.getStatus() != 3 || sku.getStatus() != 3) {
                throw new ApiException("商品[" + sku.getProdName() + "]已下架");
            }
            //平台使用费 + 站点费用 +站点运费 SKU
            double sukPlatform = Arith.add(sku.getPlatformFee(), product.getSiteFee());
            double sukPrice = 0;
            int mallVip = mallUserVipService.selectUserVip(SecurityUtils.getUserId(), product.getShopId());
            if (1 == mallVip) {
                sukPrice = sku.getVipPrice();
            } else {
                sukPrice = sku.getPrice();
            }
            totalCount = shopCartItem.getProdCount() + totalCount;
            total = Arith.add(shopCartItem.getProductTotalAmount(), total);
            shopCartItem.setActualTotal(sukPrice);
            shopCartOrderDto.setActualTotal(total);
            shopCartOrderDto.setTotal(total);
            shopCartOrderDto.setTotalCount(totalCount);

        }
    }




    /*@EventListener(ConfirmOrderEvent.class)
    @Order(ConfirmOrderOrder.DEFAULT)
    public void defaultConfirmOrderEvent(ConfirmOrderEvent event) {
        ShopCartOrderDto shopCartOrderDto = event.getShopCartOrderDto();
        OrderParam orderParam = event.getOrderParam();
        Long userId = SecurityUtils.getUserId();
        // 订单的地址信息
        //UserAddr userAddr = userAddrService.getUserAddrByUserId(orderParam.getAddrId(), userId);
        double total = 0.0;
        int totalCount = 0;
        double transfee = 0.0;
        for (ShopCartItemDto shopCartItem : event.getShopCartItems()) {
            // 获取商品信息
            Product product = productService.getProductByProdId(shopCartItem.getProdId());
            // 获取sku信息
            Sku sku = skuService.getSkuBySkuId(shopCartItem.getSkuId());
            if (product == null || sku == null) {
                throw new ApiException("购物车包含无法识别的商品");
            }
//            if (product.getStatus() != 3 || sku.getStatus() != 3) {
//                throw new ApiException("商品[" + sku.getProdName() + "]已下架");
//            }
            //totalCount = shopCartItem.getProdCount() + totalCount;
            //total = Arith.add(shopCartItem.getProductTotalAmount(), total);
            // 用户地址如果为空，则表示该用户从未设置过任何地址相关信息
//            if (userAddr != null) {
//                // 每个产品的运费相加
//                Double aDouble = transportManagerService.calculateTransfee(shopCartItem, userAddr);
//                if (0 == orderParam.getShippingType()) {
//                    aDouble = 0.0;
//                }
//                transfee = Arith.add(transfee, aDouble);
//            }


            double sukPlatform = Arith.add(Arith.add(sku.getPlatformFee(), product.getCheckFee()), product.getSiteFee());

            //站点费用
            Double siteFee = product.getSiteFee();
            //商品价格
            Double skuPrice = null;
            if (orderParam.getMallVip() != null && 1 == orderParam.getMallVip()) {
                skuPrice = sku.getVipPrice();
            } else {
                skuPrice = sku.getPrice();
            }
            //托运费用
            Double checkFee = product.getCheckFee();
            transfee = Arith.add(transfee, checkFee);



            shopCartItem.setActualTotal(Arith.add(skuPrice, sukPlatform));

            shopCartOrderDto.setActualTotal(Arith.add(skuPrice, skuPrice));


            shopCartOrderDto.setTotal(total);


            //total = Arith.add(Arith.add(platform, skuPrice), product.getCheckFee());

            total = sukPlatform;
           // total = Arith.add(platform, transfee);
            shopCartOrderDto.setTotalCount(totalCount);
            shopCartOrderDto.setTransfee(transfee);
        }
    }*/
}
